import {unioformDateTransform} from '../../utils/index.js'
import {handleSql} from '../../database/mysql/index.js'


class aiConversionModel {
    constructor() {
        this.tableName = 'ai_conversition';
    }
   
    // 查询用户对应角色下的对话信息
    async getConversationByUserAndRole({userId, roleCode}) {
         const sql = `SELECT sessionId,sessionContent,time FROM ${this.tableName} 
                     WHERE userId = ? AND roleCode = ?`
        let result = await handleSql(sql,[userId,roleCode])
        return result

    }

    // 新增对话信息
    async addConversation(conversationData) {
        const {userId,roleCode,sessionId,sessionContent} = conversationData
        let time = unioformDateTransform(new Date())
        const sql = `INSERT INTO ${this.tableName}(userId,roleCode,sessionId,sessionContent,time)  
                     VALUES (?,?,?,?,?)`
        let result = await handleSql(sql,[userId,roleCode,sessionId,JSON.stringify(sessionContent),time])
        if(result.affectedRows == 1){
            return true
        }else{
            return false
        }
    }

    // 删除用户对应角色下的对话信息
    async deleteConversationByUserAndRole({userId, roleCode}) {
        const sql = `DELETE FROM ${this.tableName} WHERE userId = ? AND roleCode = ?`;
        let result =  await handleSql(sql, [userId,roleCode]);
        if(result.affectedRows > 0){
            return true
        }else{
            return false
        }
    }   
}   

export default new aiConversionModel();